package com.example.controller;

import com.example.common.Result;
import com.example.entity.News;
import com.example.service.NewsService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

/**
 * 公告信息(News)表控制层
 */
@CrossOrigin
@RestController
public class NewsController {
    
    @Resource
    private NewsService newsService;
    
    // 信息列表
    @ResponseBody
    @RequestMapping("newsList")
    public Result newsList(@RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum,
                          @RequestParam(defaultValue = "1", value = "pageSize") Integer pageSize,
                          HttpServletRequest request) {
        String key = request.getParameter("key");
        
        PageInfo<News> pageInfo = newsService.getNewsList(pageNum, pageSize, key);
        return Result.success(pageInfo);
    }

    // 添加信息
    @ResponseBody
    @RequestMapping("newsAdd")
    public Result newsAdd(News news, HttpServletRequest request) {
        newsService.saveOrUpdate(news);
        return Result.success();
    }

    // 编辑信息
    @ResponseBody
    @RequestMapping("newsEdit")
    public Result newsEdit(News news, HttpServletRequest request) {
        newsService.saveOrUpdate(news);
        return Result.success();
    }
    
    // 查询信息详情
    @ResponseBody
    @RequestMapping("newsDateil")
    public Result newsDateil(int id, HttpServletRequest request) {
        HashMap<String, Object> res = newsService.getNewsDetail(id);
        return Result.success(res);
    }

    // 删除信息
    @ResponseBody
    @RequestMapping("newsDel")
    public Result newsDel(int id, HttpServletRequest request) {
        newsService.deleteNews(id);
        return Result.success();
    }

    // 批量删除信息
    @ResponseBody
    @RequestMapping("newsDeleteAll")
    public Result typeDeleteAll(HttpServletRequest request) {
        String vals = request.getParameter("vals");
        newsService.batchDeleteNews(vals);
        return Result.success();
    }
}

